package com.yocard.yxyd.ui.presenter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.net.Uri;
import android.widget.ImageView;

import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yocard.yxyd.R;
import com.yocard.yxyd.base.AppContext;
import com.yocard.yxyd.base.CommonSubscriber;
import com.yocard.yxyd.base.Constants;
import com.yocard.yxyd.base.RxPresenter;
import com.yocard.yxyd.http.HttpManager;
import com.yocard.yxyd.http.model.response.CommonBean;
import com.yocard.yxyd.others.GifSizeFilter;
import com.yocard.yxyd.others.Glide4Engine;
import com.yocard.yxyd.ui.contract.ChargeContract;
import com.yocard.yxyd.utils.CommonUtils;
import com.yocard.yxyd.utils.RxUtil;
import com.yocard.yxyd.utils.util.ToastUtils;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.filter.Filter;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import org.json.JSONArray;

import java.io.File;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created on 2019/4/9.
 */
public class ChargePresenter extends RxPresenter<ChargeContract.View> implements ChargeContract.Presenter {
    // 退单照片上传attachType
    private static final String REFUND_TYPE = "Refund";

    @Inject
    public ChargePresenter(HttpManager manager) {
        super(manager);
    }

    @Override
    public void selectImage(Activity activity, int requestCode) {
        try {
            AndPermission.with(AppContext.getContext())
                    .runtime()
                    .permission(Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_EXTERNAL_STORAGE)
                    .onGranted(permissions -> {
                        Matisse.from(activity)
                                .choose(MimeType.ofImage())
                                .theme(R.style.Matisse_Dracula)
                                .countable(false)
                                .capture(true) //是否提供拍照功能
                                .captureStrategy(new CaptureStrategy(true,mView.getContext().getPackageName() + ".fileprovider"))//存储到哪里
                                .addFilter(new GifSizeFilter(320, 320, 5 * Filter.K * Filter.K))
                                .maxSelectable(1)
                                .originalEnable(true)
                                .maxOriginalSize(10)
                                .imageEngine(Glide4Engine.getInstance())
                                .forResult(requestCode);
                    })
                    .onDenied(permissions -> {
                        ToastUtils.showLong("请授予APP相应权限");
                    })
                    .start();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @SuppressLint("CheckResult")
    @Override
    public void uploadImage(String type, String fileUrl, ImageView iv, Uri uri) {
        addSubscribe(Observable.create(new ObservableOnSubscribe<File>() {
            @Override
            public void subscribe(ObservableEmitter<File> emitter) throws Exception {
                //压缩反而占用大量内存，oom
//                File file = CommonUtils.saveImg(fileUrl, type);
                File file = CommonUtils.saveImg(Constants.DEFAULT_PIC_SIZE, fileUrl, type);
                emitter.onNext(file);
            }
        })
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(file -> {
                    addSubscribe(httpManager.uploadImg(type, file, REFUND_TYPE)
                            .compose(RxUtil.rxSchedulerHelper())
                            .subscribeWith(new CommonSubscriber<CommonBean>(mView) {
                                @Override
                                public void onSuccess(CommonBean data) {
                                    try {
                                        mView.uploadSuccess(type, data.getId(), iv, uri);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            }));
                }));


    }

    @Override
    public void commitRefund(String id, String mark, List<String> urls) {
        addSubscribe(httpManager.commitRefund(id, mark, urls)
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<Object>>(mView) {
                    @Override
                    public void onSuccess(List<Object> data) {
                        mView.onCommitSuccess();
                    }
                }));
    }
}
